/**
 * This class is bound to movie clips on stage, and detects the name of the event
 * 
 * This is typically subclassed by application specific events on given frames
 * to notify the engine what to do, this enabled non-scriptors to communicate with 
 * the API.
 * 
 * @author Troy Gardner
 */

import com.troyworks.hsmf.AEvent;
import com.troyworks.hsmf.Signal;
import com.troyworks.framework.Application;
import com.troyworks.spring.Factory;
import com.troyworks.framework.events.IEventOracle;
import com.troyworks.hsmf.Hsmf;
import com.troyworks.framework.logging.SOSLogger;
import com.troyworks.framework.logging.ILogger;
import com.troyworks.framework.logging.Logger;
import com.troyworks.framework.logging.LogLevel;
class com.troyworks.framework.frametracker.FrameTracker extends MovieClip{
	var evt:AEvent;
	var logger:ILogger;
	//this has issues during the casting from the oracle, as is a MovieClip and it seems to lose it's link to 
	// the interfaces it implmeents.
	var oracle:Object; //AEventOracle;
	public static var SINGLE_EVENT:Number = 1;
	public static var DUAL_EVENT:Number =2;
	public var mode:Number = FrameTracker.SINGLE_EVENT;
	//
	public var evt_name:String;
	public var evt2_name:String;
	
	private static var _a:Object = IEventOracle;
	
	//////////////////////////////////
	// This class is used with linkage ID's
	// in conjunction with the MVC separation
	// when the corresponding clip is placed on the stage
	// it will broadcast an event when it reaches t
	private function FrameTracker() {
		super();

		this.logger = ILogger(Factory.getImplementor("Logger"));
		this.logger.logLevel(LogLevel.INFO ,"new FrameTrackerfffffffffffffffffffff");
		Factory.listRegistrants();
		//var o = Factory.getImplementor("Oracle");
		//var io = AEventOracle(io); //RETURNS NULL!!! 
		//trace(AEventOracle + "o " + o + " io " + io);
		this.oracle =Factory.getImplementor("Oracle");
				this.logger.log("FT Factory? oracle?" + this.oracle);
	}
	public function onLoad():Void{
		this.onFrameChanged(true);
	}
	public function onUnload():Void{
		if(this.mode == FrameTracker.DUAL_EVENT){
			this.onFrameChanged(false);	
		}
	}
	///////////////////////////////////
	// sets up the linkage between this class and the onstage objecr
	// should be called early on.
	public static function setupLinkage() : Void {
		Object.registerClass("FrameTracker", FrameTracker);
	}

	public function onFrameChanged(enter:Boolean) : Void {
		this.logger.logLevel(LogLevel.INFO , "*****************************************");
		this.logger.logLevel(LogLevel.INFO ,"FrameTracker evt_name " + this.evt_name +  " " + enter);
		this.logger.logLevel(LogLevel.INFO ,"**parent "+this._parent+"***** "+this._name+" ***** "+this._parent._currentframe+"*****************************");
		this.logger.logLevel(LogLevel.INFO ,"*****************************************");
		var evt:AEvent = new AEvent();
		evt.sig = Hsmf.USER_SIG;
		var args = new Object();
		args.parentname = String(this._parent._name);
		args.target = this._parent;
		args.name = ((this.evt_name!=null)?String(this.evt_name):String(this._name))+((enter)?"":"ed");
		args.currentframe = Number(this._parent._currentframe); 
		evt.args = args;
		this.oracle.dispatch(evt);	
	}

}